package com.hle;


public class BruteForce01 {
	
	public static int indexOf2(String text, String pattern) {
		if (text == null || pattern == null) return -1;
		int tlen = text.length();
		int plen = pattern.length();
		if (tlen == 0 || plen == 0 || plen > tlen) return -1;
		int pi = 0, ti = 0;
		
		while (pi < plen && ti - pi <= tlen - plen) {
			if (text.charAt(ti) == pattern.charAt(pi)) {
				ti++;
				pi++;
			}else {
				ti -= pi - 1;
				pi = 0;
			}
		}
		return pi == plen ? ti - pi : -1;
	}
	
	
	public static int indexOf1(String text, String pattern) {
		if (text == null || pattern == null) return -1;
		int tlen = text.length();
		int plen = pattern.length();
		if (tlen == 0 || plen == 0 || plen > tlen) return -1;
		int pi = 0, ti = 0;
		
		while (pi < plen && ti < tlen) {
			if (text.charAt(ti) == pattern.charAt(pi)) {
				ti++;
				pi++;
			}else {
				ti -= pi - 1;
				pi = 0;
			}
		}
		return pi == plen ? ti - pi : -1;
	}
	
}
